import eventBus from "../../eventBus/eventBus.js";

async function login(username, password) {
  const formData = { username, password };
  const { data } = await eventBus.$axios.post("/login", formData);
  if (data.token) {
    window.localStorage.setItem("weiyi_token", data.token);
    eventBus.$axios.defaults.headers["authorization"] = `Bearer ${data.token}`;
  }
  return data;
}

async function sendFormData(url, formData) {
  const { data } = await eventBus.$axios({
    url,
    data: formData,
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
  return data;
}

async function sendApplicationData(url, formData) {
  const { data } = await eventBus.$axios({
    url,
    data: formData,
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  });
  if (data) {
    return data;
  }
}
export { login, sendFormData, sendApplicationData };
